package tchatcorba.tchat;

import org.omg.CORBA.ORB;
import org.omg.CORBA.ORBPackage.InvalidName;
import org.omg.CosNaming.NameComponent;
import org.omg.CosNaming.NamingContextExt;
import org.omg.CosNaming.NamingContextExtHelper;
import org.omg.CosNaming.NamingContextPackage.CannotProceed;
import org.omg.CosNaming.NamingContextPackage.NotFound;
import org.omg.PortableServer.POA;
import org.omg.PortableServer.POAHelper;
import org.omg.PortableServer.POAManagerPackage.AdapterInactive;
import org.omg.PortableServer.POAPackage.ServantNotActive;
import org.omg.PortableServer.POAPackage.WrongPolicy;

import tchatcorba.ServeurInterface;
import tchatcorba.ServeurInterfaceHelper;



/**
 * Tchat en Corba
 * @author Toto
 * @version 1.0
 */
public class Client {

	/**
	 * 
	 * @param args
	 */
	public static void main (String[] args) {		

		String nomClient;

		IHM ihm = new IHM();

		System.out.print("Votre pseudo : ");
		nomClient = LectureChaine.lireChaine();

		try {

			//Initialisation de l'ORB
			ORB orb = ORB.init(args, null);

			//Recuperation de reference de l'adaptateur d'objets racine
			POA rootpoa = POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
			rootpoa.the_POAManager().activate();

			//Initialisation de l'objet servant
			ClientImpl clientChat = new ClientImpl(nomClient, ihm);;

			//Recuperation de la reference du servant
			org.omg.CORBA.Object ref = rootpoa.servant_to_reference(clientChat);
			//Forum href = ForumHelper.narrow(ref);

			//Recuperation de la reference de l'objet mandataire (proxy) du service de nommage 
			org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService");
			//Conversion
			NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);

			//"Enregistrement" dans le service de nommage
			String name = "nomClient";
			NameComponent path[] = ncRef.to_name(name);
			//ncRef.rebind(path, href);
			ncRef.rebind(path, ref);

			
			Thread threadRecevoir = new Thread (new ClientReceptionMessage(nomClient, ihm, orb));
			threadRecevoir.start();


			ihm.go();


			//Recuperation de la reference de l'objet mandataire (proxy) du service de nommage 
			objRef = orb.resolve_initial_references("NameService");
			ncRef = NamingContextExtHelper.narrow(objRef);
			//Recuperation de la reference du servant grace au service de nommage 
			name = "serveurtchat";
			ServeurInterface serveurChat = ServeurInterfaceHelper.narrow(ncRef.resolve_str(name));

			serveurChat.enregistrer(nomClient);


			Thread threadEnvoyer = new Thread (new ClientEnvoyerMessage(nomClient, ihm, serveurChat));
			threadEnvoyer.start();

			
			
		} catch (InvalidName e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NotFound e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (CannotProceed e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (org.omg.CosNaming.NamingContextPackage.InvalidName e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (AdapterInactive e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ServantNotActive e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (WrongPolicy e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}


}


